package com.dx.mes.web;

import com.dx.mes.common.transcoder.util.JacksonUtil;
import com.dx.mes.facade.warehouse.tx.Mwpwhsmtrl.MwpwhsmtrlOA;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;

import com.opencsv.CSVReader;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by admin on 2018/3/14.
 */
@Controller
public class MwpwhsmtrlController {
    @RequestMapping(value = "/csvUpload.do")
    @ResponseBody
    private String getCsvReader(@RequestParam(value = "file",required = false) CommonsMultipartFile file,
                                   Model model, HttpServletResponse response, HttpServletRequest request) throws Exception{

        InputStream inputStream = file.getInputStream();
        InputStreamReader is = new InputStreamReader(inputStream, "UTF-8");
        BufferedReader reader = new BufferedReader(is);
        CSVReader csvReader = new CSVReader(reader);
        List<String[]> allList = csvReader.readAll();
        List<MwpwhsmtrlOA>   list =  new ArrayList<>();
            for(int i=1;i<allList.size();i++){
                MwpwhsmtrlOA prdInfo = new MwpwhsmtrlOA();
                    prdInfo.setVrd_prd_seq_id(allList.get(i)[0]);
                    prdInfo.setMtrl_box_id(allList.get(i)[1]);
                    prdInfo.setBox_cate(allList.get(i)[2]);
                    prdInfo.setCus_id_fk(allList.get(i)[3]);
                    prdInfo.setMtrl_prod_id(allList.get(i)[4]);
                    prdInfo.setMtrl_grade(allList.get(i)[5]);
                    prdInfo.setMtrl_def_code(allList.get(i)[6]);
                    prdInfo.setMtrl_pallet_id(allList.get(i)[7]);
                    prdInfo.setMtrl_lot_id(allList.get(i)[8]);
                    prdInfo.setStb_shop(allList.get(i)[9]);
                    prdInfo.setStorage_loc(allList.get(i)[10]);
                    prdInfo.setCrt_usr(allList.get(i)[11]);
                    prdInfo.setCrt_timestamp(allList.get(i)[12]);
                list.add(prdInfo);
            }
       return JacksonUtil.toJSONStr(list);
    }

}
